package com.s2u2m.training.ex_programmer.single_responsibility.bad;

import com.s2u2m.training.ex_programmer.single_responsibility.Item;

import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 一个类里有2个职能，计算总额和打印
 */
class ShoppingCart {
    private List<Item> items = new LinkedList<>();

    public void addItem(Item item) {
        items.add(item);
    }

    public BigDecimal getTotalPrice() {
        BigDecimal total = BigDecimal.ZERO;
        for (Item item : items) {
            total = total.add(item.getPrice());
        }
        return total;
    }

    public String print() {
        String list = items.stream()
                .map(Item::toString)
                .collect(Collectors.joining("\n"));
        String total = "Total: " + getTotalPrice();
        return String.join("\n", list, total);
    }
}
